package cn.edu.scnu.service;

import cn.edu.scnu.entity.User;
import cn.edu.scnu.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    @Autowired
    private UserMapper userMapper;

    // 注册
    public void register(String phone, String password, String userName) {
        User newUser = new User();
        newUser.setPhone(phone);
        newUser.setUserName(userName);
        newUser.setPassword(password);
        newUser.setIsVip(0);
        userMapper.insert(newUser);
    }

    // 查询用户账号信息是否存在
    public User login(String phone, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", phone);
        queryWrapper.eq("password", password);
        List<User> userList = userMapper.selectList(queryWrapper);
        if (userList.size() != 0)
            return userList.get(0);
        else
            return null;
    }



    public void updateUser(User user) {
        userMapper.updateById(user);
    }
}
